package com.its.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.its.mapper.BusClaimReportMapper;
import com.its.mapper.WorkingPersonnelMapper;
import com.its.pojo.BusClaimReport;
import com.its.pojo.BusDispatchInfo;
import com.its.mapper.BusDispatchInfoMapper;
import com.its.service.BusDispatchInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.its.web.param.BusDispatchInfoControllerParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 调度信息表 服务实现类
 * </p>
 *
 * @author 朱厚强
 * @since 2023年12月27日
 */
@Service
public class BusDispatchInfoServiceImpl extends ServiceImpl<BusDispatchInfoMapper, BusDispatchInfo> implements BusDispatchInfoService {
    @Autowired
    private BusDispatchInfoMapper mapper;
    @Autowired
    private BusClaimReportMapper claimReportMapper;
    @Autowired
    private WorkingPersonnelMapper workingPersonnelMapper;
    @Override
    public PageInfo<BusDispatchInfo> selectList(BusDispatchInfoControllerParam param) {

        // 开始分页
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        // 用lambda 表达式的方式生成
        LambdaQueryWrapper<BusDispatchInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 报案编号 需要数据库中的字段与实体类的属性进行相匹配
        if (StringUtils.isNotBlank(param.getClaimReportId())){
            queryWrapper.eq(BusDispatchInfo::getClaimReportId,param.getClaimReportId());
        }
        // 状态正常的
        queryWrapper.eq(BusDispatchInfo::getAssignStatus,param.getAssignStatus());
        // 查勘状态为 未查勘的
        queryWrapper.eq(BusDispatchInfo::getSurveryState,0);
        // 报案时间
        if (param.getAssignTime() != null){
            // 时间mysql，每年日时分秒 查询 年月日
            queryWrapper.between(BusDispatchInfo::getAssignTime,param.getAssignTime()+" 00:00:00",param.getAssignTime()+" 23:59:59");
        }
        // 排序 根据报案时间进行排序
        queryWrapper.orderByDesc(BusDispatchInfo::getAssignTime);
        List<BusDispatchInfo> list = mapper.selectList(queryWrapper);
        // 处理关联信息
        list.forEach(dList ->{
            // 保存报案信息
            dList.setClaimReport(claimReportMapper.selectById(dList.getClaimReportId()));
            // 保存被指派人信息
            dList.setSurveyor(workingPersonnelMapper.selectById(dList.getSurveyorId()));
            // 保存调度员信息
            dList.setDispatcher(workingPersonnelMapper.selectById(dList.getDispatcherId()));
        });
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public boolean delete(BusDispatchInfo busDispatchInfo) {
        try {
            // 将调度表状态更改为已撤销、撤销时间为现在 、以及加上撤销原因
            busDispatchInfo.setAssignStatus(0).setRevokeTime(LocalDateTime.now()).setRevokeReason(busDispatchInfo.getRevokeReason());
            mapper.updateById(busDispatchInfo);
            // 更改报案表里的流程状态
            BusClaimReport claimReport = new BusClaimReport().setClaimReportId(busDispatchInfo.getClaimReport().getClaimReportId()).setFlowState(0);
            claimReportMapper.updateById(claimReport);
            return true;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动回滚
            System.out.println("撤回调度信息时出现异常"+e.getStackTrace());
            return false;
        }
    }

    @Override
    @Transactional
    public Boolean addDispatch(BusDispatchInfo dispatchInfo) {
        try {
            dispatchInfo.setAssignTime(LocalDateTime.now());// 指派时间
            dispatchInfo.setAssignStatus(1); // 调度信息为正常
            dispatchInfo.setSurveryState(1);// 修改调度表查勘状态为查勘中
            mapper.insert(dispatchInfo);
            // 修改报案表状态
            claimReportMapper.updateById(new BusClaimReport().setClaimReportId(dispatchInfo.getClaimReportId()).setFlowState(2));
            return true;
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); // 手动回滚
            System.out.println("指派任务时出现异常"+e.getStackTrace());
            return false;
        }

    }

    @Override
    public boolean selectTaskS(BusClaimReport claimReport) {
//        System.out.println("-------------------------------------------"+claimReport);
        // 更改流程状态
        claimReport.setFlowState(1);
        int i = claimReportMapper.updateById(claimReport);
        if (i>0){
            return true;
        }
        return false;
    }

    @Override
    public List<BusClaimReport> queryTask(Integer dispatchId) {
        System.out.println("-----------------------"+dispatchId);
        // 用lambda 表达式的方式生成
        LambdaQueryWrapper<BusClaimReport> queryWrapper = new LambdaQueryWrapper<>();
        // 调度员id
        if (dispatchId != null){
            queryWrapper.eq(BusClaimReport::getDispatchId,dispatchId);
        }
        // 查询状态为指派中的数据
        queryWrapper.eq(BusClaimReport::getFlowState,1);
        // 按照报案时间 降序 排序
        queryWrapper.orderByDesc(BusClaimReport::getReportTime);
        return claimReportMapper.selectList(queryWrapper);
    }
}
